package app.nexd.com.indoor.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import app.nexd.com.indoor.R;
import app.nexd.com.indoor.activity.inter.INexdMapViewActivity;
import app.nexd.com.indoor.application.MyApplication;
import app.nexd.com.indoor.mode.FloorModel;
import app.nexd.com.indoor.mode.MallModel;
import app.nexd.com.indoor.presenter.INexdMapViewPresenterImpl;
import app.nexd.com.indoor.presenter.inter.INexdMapViewPresenter;
import app.nexd.com.indoor.util.ProgressDialogUtils;
import app.xu.com.listtext.listener.OnTextItemClickListener;
import app.xu.com.listtext.view.ListTextView;
import cn.nexd.map.rendering.SVGMapViewListener;
import cn.nexd.map.rendering.core.componet.SVGMapView;

/**
 * NexdSvgMapView，用于显示室内地图信息
 * Created by lawrence on 2015/12/4.
 */
public class NexdMapViewActivity extends Activity implements INexdMapViewActivity, OnTextItemClickListener, View.OnClickListener {
    private static final String TAG = NexdMapViewActivity.class.getSimpleName();
    INexdMapViewPresenter iNexdMapViewPresenter;
    SVGMapView nexdMap;
    TextView title_filter_text;
    ListTextView listTextView;
    List<FloorModel> floorModels;
    List<String> itemNames;
    ImageButton title_left_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nexd_map_layout);
        nexdMap = (SVGMapView) this.findViewById(R.id.svg_map);
        title_filter_text = (TextView) this.findViewById(R.id.title_filter_text);
        listTextView = (ListTextView) this.findViewById(R.id.list_text);
//        listTextView.setHeight(200);
        title_left_btn = (ImageButton) this.findViewById(R.id.title_left_btn);
        title_left_btn.setOnClickListener(this);
        Intent intent = getIntent();
        MallModel mallModel = intent.getParcelableExtra("MallModel");
        floorModels = intent.getParcelableArrayListExtra("FloorModels");
        listTextView.setOnTextItemClickListener(this);
        // 实例化业务接口
        iNexdMapViewPresenter = new INexdMapViewPresenterImpl(nexdMap, this, mallModel, this);

        title_filter_text.setText(mallModel.getName());
        initListItemData();
        listTextView.setSelection(ListTextView.DEFAULT_SELECTED_ITEM);
        // 初始化地图
        iNexdMapViewPresenter.initMap();
        nexdMap.registerMapViewListener(new MySvgMapViewListener());
        // 初始化加载第一张地图
        if (floorModels != null || floorModels.size() != 0) {
            iNexdMapViewPresenter.loadMap(floorModels.get(0));
        } else {
            showToast("获取楼层信息出错！");
        }
    }

    private void initListItemData() {
        if (itemNames == null) {
            itemNames = new ArrayList<>();
        }

        itemNames.clear();
        for (FloorModel floorModel : floorModels) {
            itemNames.add(floorModel.getFloorName());
        }

        listTextView.setItems(itemNames);
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void loadMap(String svgPath) {
        try {
            nexdMap.loadMap(new FileInputStream(new File(svgPath)));
        } catch (FileNotFoundException e) {
            MyApplication.getInstance().reportException(e);
        }
    }

    @Override
    public void refreshNexdMap() {
        nexdMap.refresh();
    }

    @Override
    public void setCurrentFloor(String svgPath) {
        try {
            nexdMap.setMaxZoomValue(300);
            nexdMap.setCurrentFloor(new FileInputStream(new File(svgPath)), new MySvgMapViewListener());
        } catch (FileNotFoundException e) {
            MyApplication.getInstance().reportException(e);
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        iNexdMapViewPresenter.startLocation();
    }

    @Override
    public void onItemClickListener(int which, String itemName) {
        iNexdMapViewPresenter.setCurrentFloor(floorModels.get(which));
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    public void getMapView(View view) {
        nexdMap.getCurrentMap();
    }

    private class MySvgMapViewListener implements SVGMapViewListener {

        @Override
        public void onMapLoadComplete(long floorId, final String buildName) {
            if (nexdMap.isMapLoadFinsh()) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d("MySvgMapViewListener", "地图加载完成");
                        iNexdMapViewPresenter.setLocationConfig();
                        iNexdMapViewPresenter.startLocation();
                        ProgressDialogUtils.dismissProgressDialog(NexdMapViewActivity.this);
                    }
                });
            } else {
                Log.d("MySvgMapViewListener", "加载未完成");
            }
        }

        @Override
        public void onMapLoadError() {
            showToast("地图加载失败，请检查您的数据!");
        }

        @Override
        public void onGetCurrentMap(Bitmap bitmap) {
            iNexdMapViewPresenter.saveNexdMap(bitmap);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        iNexdMapViewPresenter.stopLocation();
    }
}
